package com.manage.controller;

import com.beans.NewsContent;
import com.beans.page.PageList;
import com.manage.util.UserUtils;
import com.services.NewsContentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author jiangwei
 * @since 18/1/23
 */
@Controller
@RequestMapping("newsContent")
public class NewsContentController extends BaseController{
    @Autowired
    private NewsContentService newsContentService;

    /**
     * 查询列表
     *
     * @param newsContent
     * @return
     */
    @RequiresPermissions("newsContent:view")
    @RequestMapping(value = {"", "/", "list"})
    public String list(NewsContent newsContent, HttpServletRequest request) {

        newsContent.setCreateBy(UserUtils.getUser().getUserId());
        request.setAttribute("newsContent", newsContent);
        return "newsContent/list";
    }

    /**
     * 异步获取list资源
     *
     * @param pageList
     * @param newsContent
     * @return
     */
    @RequiresPermissions("newsContent:view")
    @RequestMapping("asynList")
    @ResponseBody
    public PageList asynList(PageList pageList, NewsContent newsContent) {
        return newsContentService.queryPage(pageList, newsContent);
    }

    /**
     * 新增修改
     *
     * @param newsContent
     * @return
     */
    @RequiresPermissions("newsContent:edit")
    @RequestMapping(value = {"add", "edit"}, method = RequestMethod.GET)
    public String edit(NewsContent newsContent, Model model){
        if(newsContent.getNewsId() != null){
            newsContent = newsContentService.queryById(newsContent.getNewsId());
        }

        model.addAttribute("newsContent", newsContent);

        return "newsContent/edit";
    }

    /**
     * 详情
     *
     * @param newsContent
     * @return
     */
    @RequiresPermissions("newsContent:detail")
    @RequestMapping("detail")
    public String detail(NewsContent newsContent, Model model){
        if(newsContent.getNewsId() != null){
            newsContent = newsContentService.queryById(newsContent.getNewsId());
        }

        model.addAttribute("newsContent", newsContent);

        return "newsContent/detail";
    }

    @RequiresPermissions("newsContent:edit")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(NewsContent newsContent, HttpServletRequest request){
        int result = -1;
        newsContent.setCreateBy(UserUtils.getUser().getUserId());
        if (newsContent.getNewsId() == null) {
            result = newsContentService.add(newsContent);
        } else {
            result = newsContentService.update(newsContent);
        }

        return "redirect:list";
    }

    @RequiresPermissions("newsContent:delete")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long newsId) {
        NewsContent newsContent = new NewsContent();
        newsContent.setNewsId(newsId);
        newsContent.setUpdateBy(UserUtils.getUser().getUserId());

        int result = newsContentService.delete(newsContent);

        //TODO  成功失败结果页面
        return result;
    }

    @RequestMapping("detailOfIndex")
    public String detailOfIndex(@RequestParam(value = "newsId", required = false) Long newsId,
                                 Model model){
         NewsContent newsContent = new NewsContent();

        if(newsId != null){
            newsContent = newsContentService.queryById(newsId);
        }

        model.addAttribute("newsContent", newsContent);

        return "newsContent/detail";
    }

    @RequiresPermissions("newsContent:edit")
    @RequestMapping("send")
    @ResponseBody
    public Object send(Long newsId) {
        NewsContent newsContent = new NewsContent();
        newsContent.setNewsId(newsId);
        newsContent.setUpdateBy(UserUtils.getUser().getUserId());
        newsContent.setStatus(1);

        int result = newsContentService.update(newsContent);

        //TODO  成功失败结果页面
        return result;
    }
}
